String.prototype.substrByCharLen = function(len) {
	var length = 0;
	for ( var i = 0; i < this.length; i++) {
		iCode = this.charCodeAt(i);
		if ((iCode >= 0 && iCode <= 255)
				|| (iCode >= 0xff61 && iCode <= 0xff9f)) {
			length += 1;
		} else {
			length += 2;
		}

		if (length >= len) {
			return this.substring(0, i) + '...';
		}
	}

	return this;
};

function substringByCharLen(str, len) {
	var length = 0;
	for ( var i = 0; i < str.length; i++) {
		iCode = str.charCodeAt(i);
		if ((iCode >= 0 && iCode <= 255)
				|| (iCode >= 0xff61 && iCode <= 0xff9f)) {
			length += 1;
		} else {
			length += 2;
		}

		if (length >= len) {
			return str.substring(0, i) + '...';
		}
	}

	return str;
}

function resizeImgX(img, iwidth, iheight) {
	var _img = new Image();
	_img.src = img.src;
	if (_img.width > _img.height) {
		img.width = (_img.width > iwidth) ? iwidth : _img.width;
		img.height = (_img.height / _img.width) * img.width;
	} else if (_img.width < _img.height) {
		img.height = (_img.height > iheight) ? iheight : _img.height;
		img.width = (_img.width / _img.height) * img.height;
	} else {
		img.height = (_img.height > iheight) ? iheight : _img.height;
		img.width = (_img.width > iwidth) ? iwidth : _img.width;
	}
}

function secondsToTime(seconds) {
	var hour = Math.floor(seconds / 3600);
	var left = seconds - hour * 3600;
	var minute = Math.floor(left / 60);
	var second = left - minute * 60;
	if (minute < 10) {
		minute = '0' + minute;
	}
	if (second < 10) {
		second = '0' + second;
	}

	return hour + ':' + minute + ':' + second;
}

function rmbFormat(fen) {
	var big = Math.floor(fen / 100);
	var small = fen % 100;
	if (small < 10) {
		small = '0' + small;
	}

	if (isNaN(big)) {
		big = '--';
	}
	if (isNaN(small)) {
		small = '--';
	}

	return big + '.' + small;
}

function setElementValueByID(id, value) {
	var element = document.getElementById(id);
	if (element != null) {
		element.value = value;
	}
}

function getElementValueByID(id) {
	var element = document.getElementById(id);
	if (element != null) {
		return element.value;
	}
	return '';
}

function setElementInnerHtmlByID(id, value) {
	var element = document.getElementById(id);
	if (element != null) {
		element.innerHTML = value;
	}
}

function loadPageSection(theURL, containerID, data, isShowWaiting,
		theCompleteAct) {
	$.ajax({
		type : 'post',
		url : theURL,
		data : data,
		dataType : 'html',
		success : function(data, state, xmlhttp) {
			var container = document.getElementById(containerID);
			if (container != null) {
				container.innerHTML = data;
				goScript(data);
			}
			
			adjustHeightNonAjax();
		},
		beforeSend : function(xhr) {
			if (isShowWaiting) {
				openWaitingDialog('waitingDialog');
			}
		},
		complete : function(xmlhttpRequest, textStatus) {
			if (theCompleteAct != null) {
				theCompleteAct();
			}
			closeWaitingDialog('waitingDialog');
		}
	});
}

function callJspProcess(theURL, data) {
	$.ajax({
		type : 'post',
		url : theURL,
		data : data
	});
}

function singleCheckboxes(checkboxName, checkboxID, isCheck) {
	var checkboxElements = document.getElementsByName(checkboxName);
	for (var i = 0; i < checkboxElements.length; i++) {
		var checkboxElement = checkboxElements[i];
		if (checkboxElement.id == checkboxID) {
			if (isCheck) {
				if (checkboxElement.checked) {
					checkboxElement.checked = true;
				} else {
					checkboxElement.checked = false;
				}
			} else {
				if (!checkboxElement.checked) {
					checkboxElement.checked = true;
				}
			}
		} else {
			checkboxElement.checked = false;
		}
	}
}

function showDialog(id, title, url, width, height) {
	$.dialog.load(url, {
		id: id,
	    title: title,
	    lock: true,
	    width: width,
	    height: height
	}, false);
}

function goScript(ajaxLoadedData) {
	var regDetectJs = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
	var jsContained = ajaxLoadedData.match(regDetectJs);

	if (jsContained) {
		var regGetJS = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;

		var jsNums = jsContained.length;
		for ( var i = 0; i < jsNums; i++) {
			var jsSection = jsContained[i].match(regGetJS);

			if (jsSection[2]) {
				if (window.execScript) {
					window.execScript(jsSection[2]);
				} else {
					window.eval(jsSection[2]);
				}
			}
		}
	}
}

function adjustHeightAjax() {
	var rightHeight = $('.hider').height() + $('.position').height() + $('.tt').height() + $('.content').height();
	$('.body2').css('height', rightHeight+70);
	
	if ($('.list').height() > $('.body2').height()) {
		$('.body2').css('height', $('.list').height());
	}
}

function adjustHeightNonAjax() {
	var rightHeight = $('.hider').height() + $('.position').height() + $('.tt').height() + $('.content').height() + $('.position1').height() + 70;
	var leftHeight = $('.nav1').height() + 45;
	var nav2List = $('.list2').find('.nav2');
	nav2List.each(function(i) {
		leftHeight += $(this).height();
	});
	
	if (rightHeight > leftHeight) {
		$('.list').css('height', rightHeight);
		$('.body2').css('height', rightHeight);
	} else {
		$('.list').css('height', leftHeight);
		$('.body2').css('height', leftHeight);
	}
}

function hideFocus(tName) {
	aTag = document.getElementsByTagName(tName);
	if (window.ActiveXObject) {
		for (var i = 0; i < aTag.length; i++) {
			aTag[i].hideFocus = true;
		}
	} else {
		for (var i = 0; i < aTag.length; i++) {
			aTag[i].onfocus = function() {
				this.blur();
			};
		}
	}
}

function openWaitingDialog(dialogID) {
	$
			.dialog({
				id : dialogID,
				content : '正在刷新，请稍候...<img src=\"/safe_manage/images/waiting.gif\" width="18" height="18" />',
				lock : false,
				esc : false,
				closeText : ''
			});
}

function closeWaitingDialog(dialogID) {
	try {
		$.dialog.get(dialogID).close();
	} catch (e) {
	}
}

function changeDisplayStatus(id) {
	var element = document.getElementById(id);
	if (element != null) {
		if (element.style.display == '') {
			element.style.display = 'none';
		} else {
			element.style.display = '';
		}
	}
}

function setDisplay(id, isDisplay) {
	var element = document.getElementById(id);
	if (element != null) {
		if (isDisplay) {
			element.style.display = '';
		} else {
			element.style.display = 'none';
		}
	}
}

function clickButton(id) {
	var element = document.getElementById(id);
	if (element != null) {
		element.click();
	}
}

function isDigit(s) {
	var r, re;
	re = /\d*/i;
	r = s.match(re);
	return r == s;
}

function isLeap(year){
	if((year%4==0 && year%100!=0 ) || year%400==0){
		return true;
	} else {
		return false;
	}
}

/**
 * @param year
 * @param month
 * @param day
 * @returns {Number}
 */
function getDayPassed(year, month, day){
	var passedDay = 0;
	for(var i = 1; i<month;i++){
		if(i == 2){
			if(isLeap(year)){
				passedDay += 29;
			} else {
				passedDay += 28;				
			}
		}  else if(i == 4 || i == 6 || i == 9 || i == 11){
			passedDay += 30;
		} else {
			passedDay += 31;
		}
	}
	
	passedDay = passedDay + Number(day);

	return passedDay;
}

/**
 * 计算当年剩余天数，不包括当日。
 * @param year
 * @param month
 * @param day
 * @returns {Number}
 */
function getDayLeft(year, month, day){
	var passedDay = getDayPassed(year, month, day);
	if(isLeap(year)){
		return 366-passedDay;
	}else{
		return 365-passedDay;
	}
}

/**
 * 计算年之间的日子数
 * @param yearStart
 * @param yearEnd
 */
function getDayBetweenYear(yearStart, yearEnd){
	var start = yearStart<yearEnd?yearStart:yearEnd;
	var end = yearStart<yearEnd?yearEnd:yearStart;
	
	var dayCount = 0;
	for(var i = start; i<end;i++){
		if(isLeap(i)){
			dayCount +=366;
		} else {
			dayCount +=365;
		}
	}
	
	return yearStart<yearEnd?dayCount:0-dayCount;
}

/**
 * 获取同一年内的两个日子之间的相差日子数。
 * @param year
 * @param monthStart
 * @param dayStart
 * @param monthEnd
 * @param dayEnd
 * @returns {Number}
 */
function getDayInYear(year, monthStart, dayStart, monthEnd, dayEnd){
	var startPassed = getDayPassed(year, monthStart, dayStart);
	var endPassed =  getDayPassed(year, monthEnd, dayEnd);
	return endPassed - startPassed;
}

/**
 * 获取两个时间点之间的分钟数
 * @param yearEnd
 * @param monthEnd
 * @param dayEnd
 * @param hourEnd
 * @param minuteEnd
 * @param yearStart
 * @param monthStart
 * @param dayStart
 * @param hourStart
 * @param minuteStart
 * @returns {Number}
 */
function getMinutes(yearEnd, monthEnd, dayEnd, hourEnd, minuteEnd, yearStart, monthStart, dayStart, hourStart, minuteStart){
	var days = getDayBetweenYear(yearStart, yearEnd);
	days += getDayInYear(yearEnd, monthStart, dayStart, monthEnd, dayEnd);
	
	var hours = hourEnd - hourStart;
	var minutes = minuteEnd - minuteStart;
	
	return days*24*60 + hours*60 + minutes;
}

/**
 * 获取间隔，以日时分的形式
 * @param minutes
 * @returns {Array}
 */
function getPeriod(minutes){
	var hours = minutes / 60;
	hours = Math.floor(hours);
	var day = minutes / (24 * 60);
	day = Math.floor(day);
	return new Array(day, hours % 24, minutes % 60);
}